#include "PassIntoRunBehaviour.h"


/*
 * Prihravanie beziacemu hracovi (ak je to mozne).
 *
 * @return vrati sa true, ak sa nahralo, inak false.
 */
bool PassIntoRunBehaviour::Behave()
{
//	return false;

	if(world->me->pos.GetX() < world->variables->GetMinXPosition())
		return false;
  
	float fOurPlayerAdvantage = 0.2f;
	int n;

	for(int who = 0; who < world->friends.GetSize(); who++)
	{
		if(world->friends[who].pos.GetX() <= (world->me->pos.GetX() - 10.0f))
			continue;
		
/*		for(n = 40; n > 10;  n = n - 2) 
		{
			float pos = world->friends[who].pos.GetX() + n;
			Point destination(pos, world->friends[who].pos.GetY());
			float diff = (destination - world->friends[who].pos).GetLength();
			float fNearestEnemy = DATA_STORAGE.GetNearestEnemyDistance(destination);
			
			if(diff > (fNearestEnemy - fOurPlayerAdvantage))
				continue;
			if(GetPassBallToBehaviour()->IsBlockingEnemy(destination))
				continue;

			int stepCount = int(diff / float(world->params->server->player_speed_max));
			float power = GetKickBehaviour()->GetKickPower(destination, stepCount);
			//Point path = destination - world->ball->pos;
			//float vzdial = path.GetLength();
			
			if(power <= world->params->server->ball_speed_max) 
			{
				SKILLS_DBGN("%i: ->passing to run (really 1)", world->state->GetSimTime());
				SKILLS_DBGN("%i: ->%s player %i", world->state->GetSimTime(), world->friends[who].GetSide() == OUR ? "our" : (world->friends[who].GetSide() == THEIR ? ">>their<<" : ">>unknown<<"), world->friends[who].GetUniform());
				SKILLS_DBGN("%i: ->player pos (%3.2f, %3.2f) dest (%3.2f, %3.2f)", world->state->GetSimTime(), world->friends[who].pos.GetX(), world->friends[who].pos.GetY(), destination.GetX(), destination.GetY());

      			PassBallTo(destination, 0.2f);
				return true;
			}
		}
*/
		for(n = 40; n > 10;  n = n - 2)
		{
			float xpos = world->friends[who].pos.GetX() + n;
			float ypos = (world->me->pos.GetY() > world->friends[who].pos.GetY()) ? world->friends[who].pos.GetY() + n : world->friends[who].pos.GetY() - n;
			Point destination(xpos, ypos);
			float diff = (destination - world->friends[who].pos).GetLength();
			float fNearestEnemy = DATA_STORAGE.GetNearestEnemyDistance(destination);

			if(diff > (fNearestEnemy - fOurPlayerAdvantage)) continue;
			if(GetPassBallToBehaviour()->IsBlockingEnemy(destination)) continue;

			int stepCount = int(diff / float(world->params->server->player_speed_max));
			float power = GetKickBehaviour()->GetKickPower(destination, stepCount);
			//Point path = destination - world->ball->pos;
			//float vzdial = path.GetLength();
			
			if(power <= world->params->server->ball_speed_max) 
			{
				SKILLS_DBGN("%i: ->passing to run (really 2)", world->state->GetSimTime());
				SKILLS_DBGN("%i: ->%s player %i", world->state->GetSimTime(), world->friends[who].GetSide() == OUR ? "our" : (world->friends[who].GetSide() == THEIR ? ">>their<<" : ">>unknown<<"), world->friends[who].GetUniform());
				SKILLS_DBGN("%i: ->player pos (%3.2f, %3.2f) dest (%3.2f, %3.2f)", world->state->GetSimTime(), world->friends[who].pos.GetX(), world->friends[who].pos.GetY(), destination.GetX(), destination.GetY());

      			GetPassBallToBehaviour()->Behave(destination, 0.2f);
				return true;
			}
		}
	}

	return false;
}
